
import java.awt.Color;
import java.awt.Dialog;
import java.awt.Dimension;
import java.awt.FlowLayout;

//import java.awt.List;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
//import java.io.IOException;
//import java.net.URLConnection;
//import java.net.URLStreamHandler;

import javax.imageio.ImageIO;
import javax.swing.Box;
import javax.swing.ImageIcon;
import javax.swing.JApplet;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
//import javax.swing.SwingWorker;
import javax.swing.UnsupportedLookAndFeelException;

public class ImageUploader extends JApplet implements MouseListener{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * @param args
	 */
	
	public void init() {
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (ClassNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (InstantiationException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (IllegalAccessException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (UnsupportedLookAndFeelException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		try {
			bgImage = ImageIO.read(ImageUploader.class.getResourceAsStream(("background.jpg")));
			} 
		catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			}
		browse();
	}
	
	//Color fontColor = new Color(255,255,255);
	Color fontColorHover = new Color(0.6f, 0.6f, 0.6f, 1.0f);
	
	
	ProgressBar progres;
	JLabel progresLabel;
	//JLabel status;
	File imgFile;
	BufferedImage bgImage;

	
//	public void paint(Graphics g) {
//		super.paintComponents(g);
//		if(bgImage != null) { 
//			int x = 0, y = 0; 
//			while(y < this.getHeight()) { 
//				x = 0; 
//		        while(x< this.getWidth()) { 
//		            g.drawImage(bgImage, x, y, this); 
//		            x=x+bgImage.getWidth(null); 
//		            } 
//		        y=y+bgImage.getHeight(null); 
//				}
//			this.progresLabel.setOpaque(false);
//			this.progresLabel.repaint();
//			
//		   }
//		else {
//
//			}
//		}
	
	public void browse() {
		
		this.setLayout(null);
		//this.add(new JLabel("z"));
		//FileBrowser browse = new FileBrowser(new JFrame(""));
		
		
		this.setItems();
		
		//path.setText(readFile("/media/Local/wMap/Eclipse/ImageUploader/src/file.txt"));
		
		
	}

	private void setItems() {		
		JLabel back = new JLabel();
		back.setLayout(new FlowLayout());
		back.setIcon(new ImageIcon(this.bgImage));
		
		this.progresLabel = new JLabel(this.getParameter("label"));
		progresLabel.setForeground(Color.white);
		progresLabel.addMouseListener(this);		
		back.add(this.progresLabel);
		back.add(Box.createRigidArea(new Dimension(5,0)));
		
		this.progres = new ProgressBar(new Dimension(140, 10));
		this.progres.addMouseListener(this);
		back.add(this.progres);
		//this.paint(this.getGraphics());
		
		
		progresLabel.revalidate();
		progresLabel.repaint();
		
		//this.status = new JLabel("0/0");
		back.setBounds(0, 0, 160, 50);
		this.add(back);
		
		}
	
	private void displayImage() { 		

		}
		
	
	public void setFile(File file) {
		imgFile = file;
		//path.setText(file.getPath());
		displayImage();		
		}

	

	
	public void mouseClicked(MouseEvent arg0) {
		ImageEditor editor = new ImageEditor(this);		
		editor.setModalityType(Dialog.ModalityType.APPLICATION_MODAL);
		editor.setVisible(true);	
		EditedImage[] images = editor.getImages();		
		
		if (images == null)
			return;
		
		SyncFlag flag = new SyncFlag();
		
		
		
		for (int i=0; i<images.length; i++) { 
			UploadImage uploader = new UploadImage(images[i], this, this.getParameter("uploader"));
			uploader.flag = flag;
			uploader.ukPart = images.length;
			uploader.part = i;
			uploader.start();
			while(uploader.done == "-1") {
				flag.get("glavna");			
				this.progres.setProgress(uploader.progres);
				}
			
			if (uploader.done != "false")
				this.prikazi(uploader.done);
		}
				
		
		this.progres.setProgress(1.0f);
		
	}


	private void prikazi(String path) {		
		try {			
			String sUrl = "javascript:"+this.getParameter("feedback")+"('" + path +"')";
			//JOptionPane.showMessageDialog(null, sUrl);
			URL callF = new URL(sUrl);
			getAppletContext().showDocument(callF);
			} 
		catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			JOptionPane.showMessageDialog(null, e.getMessage());
			e.printStackTrace();
			}	
		catch (Exception e) {
			JOptionPane.showMessageDialog(null, e.getMessage());
			e.printStackTrace();
			}
		}
	@Override
	public void mouseEntered(MouseEvent arg0) {
		this.progresLabel.setForeground(this.fontColorHover);
		
	}

	@Override
	public void mouseExited(MouseEvent arg0) {
		this.progresLabel.setForeground(Color.white);
		
	}

	@Override
	public void mousePressed(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	
	
}
